package account;

import java.time.Instant;

import account.provider.QueryProvider;
import account.provider.RegisterProvider;
import account.provider.UpdateProvider;

/**
 * 账号类
 * 
 * @author 毛建杰
 *
 */
public class Account
{
	private int userID;
	private String userName;
	private Password password;
	private String realName;
	private String idcard;
	private Instant registerTime;
	private String registerIP;
	private String phone;
	private String email;
	private QueryProvider queryProvider;
	private UpdateProvider updateProvider;

	/**
	 * 构造方法
	 * 
	 * @param queryProvider
	 *            查询接口
	 * @param updateProvider
	 *            更新接口
	 */
	public Account(QueryProvider queryProvider, UpdateProvider updateProvider)
	{
		this.queryProvider = queryProvider;
		this.updateProvider = updateProvider;
	}

	/**
	 * 构造方法
	 * 
	 * @param userName
	 *            用户名
	 * @param queryProvider
	 *            查询接口
	 * @param updateProvider
	 *            更新接口
	 * @throws Exception
	 *             用户名不存在
	 */
	public Account(String userName, QueryProvider queryProvider, UpdateProvider updateProvider) throws Exception
	{
		this.queryProvider = queryProvider;
		this.updateProvider = updateProvider;
		this.userName = userName;
		this.userID = this.queryProvider.queryUserID(this.userName);
		if (this.userID != -1)
		{
			this.realName = this.queryProvider.queryRealName(this.userID);
			this.idcard = this.queryProvider.queryIdcard(this.userID);
			this.registerTime = this.queryProvider.queryRegisterTime(this.userID);
			this.registerIP = this.queryProvider.queryRegisterIP(this.userID);
			this.phone = this.queryProvider.queryPhone(this.userID);
			this.email = this.queryProvider.queryEmail(this.userID);
		}
		else
		{
			throw new Exception("用户名不存在");
		}
	}

	/**
	 * 注册
	 * 
	 * @param userName
	 *            用户名
	 * @param password
	 *            密码
	 * @param realName
	 *            真实姓名
	 * @param idcard
	 *            身份证号
	 * @param registerIP
	 *            注册IP
	 * @param registerProvider
	 *            注册接口
	 * @throws Exception
	 *             用户已存在
	 */
	public static Account register(String userName, String password, String realName, String idcard, String registerIP,
			RegisterProvider registerProvider, QueryProvider queryProvider, UpdateProvider updateProvider)
			throws Exception
	{
		userName = userName.toLowerCase();
		if (Account.queryUserID(userName, queryProvider) == -1)
		{
			Account account = new Account(queryProvider, updateProvider);
			account.userName = userName;
			account.password = new Password(password);
			account.realName = realName;
			account.idcard = idcard;
			account.registerIP = registerIP;
			account.registerTime = Instant.now();
			account.userID = registerProvider.createIndexInfo(account);
			registerProvider.createUserInfo(account);
			registerProvider.createUserBaseInfo(account);
			registerProvider.createSecurityInfo(account);
			return account;
		}
		else
		{
			throw new Exception("用户名已存在");
		}
	}

	/**
	 * 查询用户ID
	 * 
	 * @param userName
	 *            用户名
	 * @param queryProvider
	 *            查询接口
	 * @return
	 */
	public static int queryUserID(String userName, QueryProvider queryProvider)
	{
		return queryProvider.queryUserID(userName);
	}

	/**
	 * 获取用户ID
	 * 
	 * @return 用户ID
	 */
	public int getUserID()
	{
		return userID;
	}

	/**
	 * 获取姓名
	 * 
	 * @return 姓名
	 */
	public String getUserName()
	{
		return userName;
	}

	/**
	 * 获取密码
	 * 
	 * @return 密码
	 */
	public Password getPassword()
	{
		return password;
	}

	/**
	 * 获取真实姓名
	 * 
	 * @return 真实姓名
	 */
	public String getRealName()
	{
		return realName;
	}

	/**
	 * 获取身份证号
	 * 
	 * @return 身份证号
	 */
	public String getIdcard()
	{
		return idcard;
	}

	/**
	 * 获取注册时间
	 * 
	 * @return 注册时间
	 */
	public Instant getRegisterTime()
	{
		return registerTime;
	}

	/**
	 * 获取注册IP
	 * 
	 * @return 注册IP
	 */
	public String getRegisterIP()
	{
		return registerIP;
	}

	/**
	 * 获取手机号
	 * 
	 * @return 手机号
	 */
	public String getPhone()
	{
		return phone;
	}

	/**
	 * 设置手机号
	 * 
	 * @param phone
	 *            手机号
	 */
	public void setPhone(String phone)
	{
		updateProvider.updatePhone(userID, phone);
		this.phone = phone;
	}

	/**
	 * 获取邮箱
	 * 
	 * @return 邮箱
	 */
	public String getEmail()
	{
		return email;
	}

	/**
	 * 设置邮箱
	 * 
	 * @param email
	 *            邮箱
	 */
	public void setEmail(String email)
	{
		updateProvider.updateEmail(userID, email);
		this.email = email;
	}

	public void setQueryProvider(QueryProvider queryProvider)
	{
		this.queryProvider = queryProvider;
	}

	public void setUpdateProvider(UpdateProvider updateProvider)
	{
		this.updateProvider = updateProvider;
	}

	/**
	 * IP地址转换为长整型
	 * 
	 * @param ipAddress
	 *            IP地址
	 * @return 长整型
	 */
	public static long ip2Long(String ipAddress)
	{
		try
		{
			String[] ipAddressInArray = ipAddress.split("\\.");
			long result = 0;
			for (int i = 0; i < ipAddressInArray.length; i++)
			{
				int power = 3 - i;
				int ip = Integer.parseInt(ipAddressInArray[i]);
				result += ip * Math.pow(256, power);
			}
			return result;
		}
		catch (Exception e)
		{
			return 0;
		}
	}

	/**
	 * 长整型转换为IP地址
	 * 
	 * @param i
	 *            长整型
	 * @return IP地址
	 */
	public static String long2Ip(long i)
	{
		return ((i >> 24) & 0xFF) + "." + ((i >> 16) & 0xFF) + "." + ((i >> 8) & 0xFF) + "." + (i & 0xFF);
	}
}
